package com.itheima.reggie.web;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.BaseContext_id获取;
import com.itheima.reggie.common.R;
import com.itheima.reggie.pojo.OrderDetail_订单明细;
import com.itheima.reggie.pojo.Orders_订单;
import com.itheima.reggie.pojo.ShoppingCart_购物车;
import com.itheima.reggie.service.OrderDetailService;
import com.itheima.reggie.service.OrderService;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


/**
 * @Author: cl
 * @Date: 2022/4/5 - 04 - 05
 * @Description:
 */
@RestController
@RequestMapping("/order")
@Slf4j
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 查看所有订单
     *
     * @param page
     * @param pageSize
     * @param number
     * @param beginTime
     * @param endTime
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String number, String beginTime, String endTime) {
        log.info("page = {},pageSize = {},number = {},beginTime = {}", page, pageSize, number, beginTime);
        //构造分页构造器
        Page pageInfo = new Page(page, pageSize);

        orderService.page(pageInfo,
                Wrappers.<Orders_订单>lambdaQuery()
                        .like(StringUtils.isNotEmpty(number), Orders_订单::getNumber, number)
                        .ge(beginTime != null, Orders_订单::getOrderTime, beginTime)
                        .le(endTime != null, Orders_订单::getOrderTime, endTime)
                        .orderByAsc(Orders_订单::getOrderTime)
        );
        return R.success(pageInfo);
    }

    /**
     * 下单    数据复杂  在service中操作
     *
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders_订单 orders) {
        log.info(orders.toString());
        orderService.submit(orders);
        return R.success("下单成功");
    }

    @Autowired
    private OrderDetailService orderDetailService;
    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 再来一单(将此订单所有菜品添加到购物车)
     *
     * @param orders
     * @return
     */
    @PostMapping("/again")
    public R<String> again(@RequestBody Orders_订单 orders) {
        //获取菜品
        List<OrderDetail_订单明细> detailList = orderDetailService.list(
                Wrappers.<OrderDetail_订单明细>lambdaQuery()
                        .eq(OrderDetail_订单明细::getOrderId, orders.getId())
        );
        //复制给购物车   记得设置用户id
        List<ShoppingCart_购物车> list =
                detailList.stream().map((item) -> {
                    ShoppingCart_购物车 s = new ShoppingCart_购物车();
                    BeanUtils.copyProperties(item, s);
                    s.setUserId(BaseContext_id获取.getCurrentId());
                    return s;
                }).collect(Collectors.toList());
        shoppingCartService.saveBatch(list);
        return R.success("添加购物车成功");
    }

    /**
     * 更新订单状态
     *
     * @return 返回值
     */
    @PutMapping
    public R<String> status(@RequestBody Orders_订单 orders) {
        orderService.updateById(orders);
        return R.success("订单状态更新成功！");
    }


    /**
     * 用户订单列表
     *
     * @return  返回值
     */
    @GetMapping("/userPage")
    public R<Page> userPage(int page, int pageSize) {
        Page pageInfo = orderService.getWithDish(page, pageSize);
        return R.success(pageInfo);
    }
}
